var messenger = new Messenger('parent', 'MessengerDemo');
    messenger.listen(function (json) {
		var jsonObj = eval("("+json+")");
		var action = jsonObj.action;
		if("choiceState"==action || "getValue"==action || "choiceAllUses"==action || "removeAllUses"==action || "checkAllUsers"==action){
			var showValue = "";
			var tagName = document.getElementById(jsonObj.showId).tagName;
			if(tagName=="TEXTAREA" || tagName =="INPUT"){
				showValue = document.getElementById(jsonObj.showId).value;
			}else{
				var obj = document.getElementById(jsonObj.showId).childNodes;
				//长度减一标识减少遍历最后的div元素
				for(var i=0;i<obj.length-1;i++){
					var tempVal = obj[i].childNodes[0].nodeValue;
					showValue += tempVal+",";
				}
			}
			var hideValue = document.getElementById(jsonObj.hideId).value;
			var str = "{\"showValue\":\""+showValue+"\",\"hideValue\":\""+hideValue+"\",\"action\":\""+jsonObj.action+"\"}";
			sendMessage(str,jsonObj.iframeId);
		}else if("fillValue"==jsonObj.action){
			document.getElementById(jsonObj.hideId).value = jsonObj.hideValue;
			var tagName = document.getElementById(jsonObj.showId).tagName;
			if(tagName=="TEXTAREA" || tagName =="INPUT"){
				document.getElementById(jsonObj.showId).value = jsonObj.showValue;
				return;
			}
			//以下代码用于填充div层
			
			var showHtml = "";
			if(jsonObj.hideValue!="" && jsonObj.hideValue.indexOf(",")==-1){
				var showValue = jsonObj.showValue;
				if(showValue.indexOf("@gxfy.com")){
					showValue = showValue.replace("<","&lt;");
					showValue = showValue.replace(">","&gt;");
				}
				showHtml += "<span style='border:1px dotted red;margin:3px;padding:2px 3px 2px 3px;line-height:40px;'>"+
						showValue+"<span style='position: relative;top: -6px;right: -2px;padding:3px;' hideValue='"+jsonObj.hideValue+"'>x</span></span>";
			}else if(jsonObj.hideValue.indexOf(",")!=-1){
				var showUserStr = jsonObj.showValue.split(",");
				var hideUserStr = jsonObj.hideValue.split(",");
				for(var i=0;i<hideUserStr.length-1;i++){
					var showValue = showUserStr[i];
					if(showValue.indexOf("@gxfy.com")){
						showValue = showValue.replace("<","&lt;");
						showValue = showValue.replace(">","&gt;");
					}
					showHtml += "<span style='border:1px dotted red;margin:3px;padding:2px 3px 2px 3px;position:relative;float:left;line-height:18px;'>"+
						showValue+"<span style='position: relative;top: -6px;right: -2px;padding:3px;' hideValue='"+hideUserStr[i]+"'>x</span></span>";
				}
			}
			//这里替换div为空字符串目的在于保证html内容中只有一个div
			showHtml = showHtml.replace("<div style='clear:both;'></div>","");
			showHtml+="<div style='clear:both;'></div>";
			document.getElementById(jsonObj.showId).innerHTML = showHtml;
			var obj = document.getElementById(jsonObj.showId).childNodes;
			//长度减一标识减少遍历最后的div元素
			for(var i=0;i<obj.length-1;i++){
				var close = obj[i].childNodes[1];
				close.onclick= function(){
					var nodes = document.getElementById(jsonObj.showId).childNodes;
					var hideValue = document.getElementById(jsonObj.hideId).value;
					var attrVal = "";
					if(nodes.length==2 && hideValue.indexOf(",")==-1){
						attrVal = this.getAttribute("hideValue");
					}else{
						attrVal = this.getAttribute("hideValue")+",";
					}
					//var hideValue = document.getElementById(jsonObj.hideId).value;
					hideValue = hideValue.replace(attrVal,"");
					document.getElementById(jsonObj.hideId).value = hideValue;
					this.parentNode.parentNode.removeChild(this.parentNode);
				}
				close.onmouseover = function(){this.style.cursor = "pointer";}
				close.onmouseout = function(){this.style.cursor = "default";}
			}
		}
    });
	
function sendMessage(json,iframeId) {
	console.log(json);
    console.log(iframeId);
	messenger.targets[iframeId].send(json);
}

var SelectGXFYUsers = {
	init:function(clickIdName,showId,hideId,choiceType,many){
		new SelectGXFYUsers.newObj(clickIdName,showId,hideId,choiceType,many);
	},
	host:"http://147.1.4.53:90",
//	host:"http://147.1.6.23",
	newObj:function(clickIdName,showId,hideId,choiceType,many){
		var parentId = clickIdName+"parendId";
		var iframeId = clickIdName+"iframe";
		var MessengerName = clickIdName+"Messenger";
		var url = SelectGXFYUsers.host+"/select/iframe?showId="+showId+"&hideId="+hideId+"&choiceType="+choiceType+"&iframeId="+iframeId+"&many="+many;
		var checkIframe = document.getElementById(iframeId);
		if(checkIframe!=null){
			checkIframe.remove();
		}
		var ifr = document.createElement("iframe");
		ifr.id = iframeId;
		ifr.src = url;
		ifr.width = 0;
		ifr.height = 0;
		ifr.scrolling = "no";
		ifr.frameBorder=0;
		ifr.allowTransparency = "true";         
		document.body.appendChild(ifr);
		messenger.addTarget(document.getElementById(iframeId).contentWindow, iframeId);
		var buttonObj = document.getElementById(clickIdName);
		buttonObj.onclick=function(){
            $("#"+iframeId).attr("src",url);
            setTimeout(function(){
                messenger.targets[iframeId].send('{"action":"openWindow"}');
            },200);
		}
	}
}
